/**
 * 
 */
package ptvv.system;

import java.util.Vector;

import ptvv.automatons.Delay;
import ptvv.automatons.Dummy;

/**
 * @author 01
 * 
 */
public class Configuration extends Thread {
	public String name;
	public int count;
	public Vector<ThreadAndClass> daAL = new Vector<ThreadAndClass>();
	public Vector<PortConnect> interconnections = new Vector<PortConnect>();
	Vector<Port> intraconnections = new Vector<Port>();
	Vector<InitialCondition> initialConditions = new Vector<InitialCondition>();
	protected Trace logger = Trace.getLogger();
	public boolean shouldRepeat = false;

	public static void main(String[] args) {
		(new Thread(new Configuration())).start();
	}

	public static int countOrigInputs(Configuration config) {
		int count = 0;
		for (Port port : config.intraconnections)
			if (port.porttype == Port.type.input)
				count++;
		return count;

	}

	public static Configuration ConfigurationTwo() {
		Configuration config = new Configuration();
		config.shouldRepeat = true;

		config.daAL.add(new ThreadAndClass(new Dummy("Dummy1")));
		config.daAL.add(new ThreadAndClass(new Dummy("Dummy2")));
		// daAL.add(new ThreadAndClass(new Collector()));
		// sender, port out, receiver, port in
		config.interconnections.add(new PortConnect(0, 0, 1, 0));
		config.interconnections.add(new PortConnect(1, 0, 0, 0));

		// config.intraconnections.add(new Port(0, 0, Port.type.input));
		// config.intraconnections.add(new Port(1, 0, Port.type.output));

		// making systempublic class PortConnect {

		for (PortConnect conn : config.interconnections) {
			config.daAL.get(conn.receiver).autoclass.setInputPort(conn.portin);
			config.daAL.get(conn.sender).autoclass.setOutputPort(
					config.daAL.get(conn.receiver), conn.portout, conn.portin);

		}
		config.initialConditions.add(new InitialCondition(0, 0, "0", true));

		return config;

	}

	public static Configuration ConfigurationOne() {
		Configuration config = new Configuration();
		config.shouldRepeat = true;
		config.daAL.add(new ThreadAndClass(new Dummy("Dummy1")));
		config.daAL.add(new ThreadAndClass(new Delay("Delay")));
		// daAL.add(new ThreadAndClass(new Collector()));
		// sender, port out, receiver, port in
		config.interconnections.add(new PortConnect(0, 0, 1, 0));
		config.interconnections.add(new PortConnect(1, 0, 0, 0));

		// config.intraconnections.add(new Port(0, 0, Port.type.input));
		// config.intraconnections.add(new Port(1, 0, Port.type.output));

		// making systempublic class PortConnect {

		for (PortConnect conn : config.interconnections) {
			config.daAL.get(conn.receiver).autoclass.setInputPort(conn.portin);
			config.daAL.get(conn.sender).autoclass.setOutputPort(
					config.daAL.get(conn.receiver), conn.portout, conn.portin);

		}
		config.initialConditions.add(new InitialCondition(0, 0, "0", true));

		return config;

	}

	public void runSystem() {
		for (ThreadAndClass tac : daAL) {
			tac.autothread.start();
		}
		daAL.get(0).autoclass.addEvent("START");

		try {
			Thread.sleep(2000);

			for (ThreadAndClass tac : daAL) {
				System.out.println("Waiting for " + tac.autothread.getName());
				tac.autothread.join();
			}
			logger.log("SHUTDOWN");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
